//
//  ReadList.m
//  Leisure
//
//  Created by xalo on 16/6/29.
//  Copyright © 2016年 朱旭东. All rights reserved.
//

#import "ReadList.h"

@interface ReadList ()<UITableViewDataSource,UITableViewDelegate>
//分段视图控制器
@property (weak, nonatomic) IBOutlet UITableView *newstTableView;//最新列表
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;//混动视图
@property (weak, nonatomic) IBOutlet UITableView *hotTableView;//最热烈表
@property(nonatomic,strong)UISegmentedControl *seg;


@property(nonatomic,assign)NSInteger newStart;//最新列表数据起始位置
@property(nonatomic,assign)NSInteger newlimit;//最新列表的条数
@property(nonatomic,assign)NSInteger hotStart;//最热列表的起始位置
@property(nonatomic,assign)NSInteger hotLimit;//最热烈表的条数
@property(nonatomic,strong)NSMutableArray *newstDataSource;//最新列表数据源
@property(nonatomic,strong)NSMutableArray *hotDataSource;//最热烈表数据源

@end

@implementation ReadList

//懒加载
- (NSMutableArray *)newstDataSource
{
    if (!_newstDataSource) {
        _newstDataSource = [NSMutableArray array];
    }
    return _newstDataSource;
}

- (NSMutableArray *)hotDataSource
{
    if (!_hotDataSource) {
        _hotDataSource = [NSMutableArray array];
    }
    return _hotDataSource;
}
- (UISegmentedControl *)seg
{
    if (!_seg) {
        _seg = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
        //插入标签
        [_seg insertSegmentWithTitle:@"最新" atIndex:0 animated:YES];
        [_seg insertSegmentWithTitle:@"最热" atIndex:1 animated:YES];
        //添加点击事件
        [_seg  addTarget:self action:@selector(clickSegementController:) forControlEvents:UIControlEventValueChanged];
    }
    return _seg;
}


//分段视图控制器的点击事件
- (void)clickSegementController:(UISegmentedControl*)sender{
    
    if (sender.selectedSegmentIndex == 0) {
        [self.scrollView setContentOffset:CGPointZero animated:YES];
    }else{
        [self.scrollView setContentOffset:CGPointMake(kwidth, 0) animated:YES];    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor  grayColor];
    self.navigationItem.titleView = self.seg;
    self.automaticallyAdjustsScrollViewInsets  =NO;
    
    [self.newstTableView registerNib:[UINib nibWithNibName:@"ReadListCell" bundle:nil] forCellReuseIdentifier:@"ReadListModel_cell"];
    [self.hotTableView registerNib:[UINib nibWithNibName:@"ReadListCell" bundle:nil] forCellReuseIdentifier:@"ReadListModel_cell"];
    
    [self loadNewstTableViewDataSourceWithStart:0 limit:10];
    [self loadHotTableViewDataSourceWithStart:0 limit:10];
    
    //下拉刷新
    self.newstTableView.mj_header = [MJRefreshStateHeader  headerWithRefreshingBlock:^{
        //刷新之前清空数组
        [self.newstDataSource removeAllObjects];
        self.newStart = 0;
        [self  loadNewstTableViewDataSourceWithStart:0 limit:10];
    }];
    
    //添加上拉加载,执行完之后要结束
    self.newstTableView.mj_footer = [MJRefreshBackGifFooter  footerWithRefreshingBlock:^{
        self.newStart += 10;
        [self  loadNewstTableViewDataSourceWithStart:self.newStart limit:10];
    }];
    
    ///////////////////////////////////////////////////////
    //下拉刷新
    self.hotTableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        //刷新之前清空数组
        [self.hotDataSource removeAllObjects];
        self.hotStart = 0;
        [self  loadHotTableViewDataSourceWithStart:0 limit:10];
    }];
    
    //添加上拉加载,执行完之后要结束
    self.hotTableView.mj_footer = [MJRefreshBackGifFooter  footerWithRefreshingBlock:^{
        self.hotStart += 10;
        [self  loadHotTableViewDataSourceWithStart:self.newStart limit:10];
    }];
    
}

#pragma mark_----------数据请求------------

//加载最新列表的数据
- (void)loadNewstTableViewDataSourceWithStart:(NSInteger)start  limit:(NSInteger)limit {
    [RequestManager fetchDataWithURL:kReadListUrl parameters:@{@"typeid":@(self.typeID),@"start":@(start),@"limit":@(limit),@"sort":@"addtime"} method:@"POST" complet:^(NSData *data) {
        
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    
        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {
            ReadListModel *model = [[ReadListModel alloc] init];
            [model setValuesForKeysWithDictionary:smallDic];
            
            [self.newstDataSource  addObject:model];
        }
    } updataUI:^{
        //结束刷新
        [self.newstTableView.mj_footer  endRefreshing];
        [self.newstTableView.mj_header endRefreshing];
        //刷新UI
        [self.newstTableView  reloadData];
    }];
}
//加载最热列表的数据
- (void)loadHotTableViewDataSourceWithStart:(NSInteger)start  limit:(NSInteger)limit{
    [RequestManager fetchDataWithURL:kReadListUrl parameters:@{@"typeid":@(self.typeID),@"start":@(start),@"limit":@(limit),@"sort":@"hot"} method:@"POST" complet:^(NSData *data) {

        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        
        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {
            ReadListModel *model = [[ReadListModel alloc] init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.hotDataSource addObject:model];
        }

    } updataUI:^{
//        [self.hotTableView.mj_footer  endRefreshing];
//        [self.hotTableView.mj_header endRefreshing];
        [self.hotTableView reloadData];
    }];
    
 

}
#pragma mark----------scrollerView----------
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    //判断滚动的是不是你要滚的
    if (scrollView == self.scrollView) {
        if (self.scrollView.contentOffset.x == 0) {
            self.seg.selectedSegmentIndex = 0;
        }else if (self.scrollView.contentOffset.x == kwidth){
            self.seg.selectedSegmentIndex = 1;
        }
    }
    
}
#pragma mark ---------tableView------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.newstTableView) {
        return self.newstDataSource.count;
    }else if(tableView == self.hotTableView){
        return self.hotDataSource.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    BaseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ReadListModel_cell" forIndexPath:indexPath];

    //创建一个model来接受
    ReadListModel *model = [[ReadListModel alloc] init];

    if (tableView == self.newstTableView) {
        model = self.newstDataSource[indexPath.row];
    }else {
        model = self.hotDataSource[indexPath.row];
    }
    
//    BaseCell *cell = [FactoryMode productCellWithModel:model tableVIew:tableView indexPath:indexPath];
    
    [cell setCellWithMode:model];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    ReadListInfoViewController *readlistinto = [[ReadListInfoViewController alloc] init];
    if (tableView == self.newstTableView) {
        readlistinto.readListModel =  self.newstDataSource[indexPath.row];
    }else{
        readlistinto.readListModel = self.hotDataSource[indexPath.row] ;
    }
    
    [self.navigationController pushViewController:readlistinto animated:YES];
}
@end
